package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Loser
 * @Date: Created in 2020-10-15 13:26
 * @Description: 菜单表现层
 * @Path: com.itheima.controller.MenuController
 * @Modified By:
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 添加菜单项
     * @param menu
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu){
        try{
            menuService.add(menu);
            return new Result(true,"添加菜单项成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加菜单项失败",null);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult = menuService.findPage(queryPageBean);
            return new Result(true,"分页查询菜单项成功",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"分页查询菜单项失败",null);
        }
    }

    /**
     * 修改数据
     * @param menu
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody Menu menu){
        try{
            menuService.edit(menu);
            return new Result(true,"修改菜单项成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改菜单项失败",null);
        }
    }

    /**
     * 根据id查询，数据回显
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id){
        try{
            Menu menul = menuService.findById(id);
            return new Result(true,"回显菜单项成功",menul);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"回显菜单项失败",null);
        }
    }

    /**
     * 删除菜单项
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Result delete(Integer id){
        try{
            menuService.delete(id);
            return new Result(true,"回显菜单项成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"回显菜单项失败",null);
        }
    }

    @RequestMapping("findAll")
    public Result findAll(){
        try{
            List<Menu> list = menuService.findAll();
            return new Result(true,"父菜单查询成功",list);
        }catch (Exception e){
            return new Result(false,"父菜单查询失败",null);
        }
    }

}
